package com.jiangyg.mall.cart.controller;

import com.jiangyg.mall.authz.MemberInfoHolder;
import com.jiangyg.mall.cart.dto.AddSkuDTO;
import com.jiangyg.mall.cart.dto.CheckedSkuDTO;
import com.jiangyg.mall.cart.dto.UpdateCountDTO;
import com.jiangyg.mall.cart.service.CartService;
import com.jiangyg.mall.cart.vo.CartVO;
import com.jiangyg.mall.core.support.restful.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 类描述：购物车控制器
 *
 * @author jiangyg
 * @version 4.0
 * @date 2022-12-07
 */
@RestController
@RequestMapping("/cart")
public class CartController {

    private final CartService cartService;

    @Autowired
    public CartController(CartService cartService) {
        this.cartService = cartService;
    }

    /**
     * 功能描述：查询购物车商品列表
     *
     * @param request 请求
     * @return 结果
     * @throws Exception
     */
    @ResponseBody
    @PostMapping("/info")
    public Result<CartVO> info(HttpServletRequest request) throws Exception {
        // 获取当前登录会员ID
        final Long memberId = MemberInfoHolder.loginMemberId();
        // 查询会员的购物车信息
        return Result.ok(cartService.info(memberId));
    }

    /**
     * 功能描述：新增商品至购物车
     *
     * @param request 请求
     * @param dto     参数
     * @return 结果
     * @throws Exception
     */
    @ResponseBody
    @PostMapping("/add")
    public Result<Object> add(HttpServletRequest request,
                              @RequestBody @Validated AddSkuDTO dto) throws Exception {
        // 获取当前登录会员ID
        final Long memberId = MemberInfoHolder.loginMemberId();
        // 新增商品至购物车
        cartService.addItem(dto, memberId);
        return Result.ok();
    }

    /**
     * 功能描述：修改购物车数量
     *
     * @param request 请求
     * @param dto     参数
     * @return 结果
     * @throws Exception
     */
    @ResponseBody
    @PostMapping("/count")
    public Result<Object> count(HttpServletRequest request,
                                @RequestBody @Validated UpdateCountDTO dto) throws Exception {
        // 获取当前登录会员ID
        final Long memberId = MemberInfoHolder.loginMemberId();
        // 修改购物车数量
        cartService.updateItemCount(dto, memberId);
        return Result.ok();
    }

    /**
     * 功能描述：删除购物车的商品
     *
     * @param request 请求
     * @param skuIds  参数
     * @return 结果
     * @throws Exception
     */
    @ResponseBody
    @PostMapping("/delete")
    public Result<Object> delete(HttpServletRequest request,
                                 @RequestBody List<Long> skuIds) throws Exception {
        // 获取当前登录会员ID
        final Long memberId = MemberInfoHolder.loginMemberId();
        // 删除购物车的商品
        cartService.deleteItem(memberId, skuIds);
        return Result.ok();
    }

    /**
     * 功能描述：勾选购物车商品
     *
     * @param request 请求
     * @param dto     参数
     * @return 结果
     * @throws Exception
     */
    @ResponseBody
    @PostMapping("/checked")
    public Result<Object> checked(HttpServletRequest request,
                                  @RequestBody @Validated CheckedSkuDTO dto) throws Exception {
        // 获取当前登录会员ID
        final Long memberId = MemberInfoHolder.loginMemberId();
        // 删除购物车的商品
        cartService.checkedItem(dto, memberId);
        return Result.ok();
    }

}
